package com.icehand.qiniu.manager;

import com.icehand.qiniu.config.QiniuConfig;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class QiniuBucketManager {
    private final BucketManager bucketManager;
    private final QiniuConfig qiniuConfig;

    public QiniuBucketManager(BucketManager bucketManager, QiniuConfig qiniuConfig) {
        this.bucketManager = bucketManager;
        this.qiniuConfig = qiniuConfig;
    }

    public String removeFile(String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        try {
            bucketManager.delete(qiniuConfig.getBucket().getName(), fileName);
            return fileName;
        } catch (QiniuException e) {
            log.error("delete file error:", e);
            throw new com.icehand.qiniu.exeception.QiniuExeception(e);
        }
    }
}